<!--
 @description 门店升级
 @fileName index.vue
 @author zengqiongying
 @created 2023/04/13 14:29:51
-->
<template>
  <div>
    <div class="ctm-content-head-fixed">
      <SearchInput
        v-model="searchValue"
        placeholder="输入市场名称"
        :show-action="false"
        @search="onSearch"
        @clear="onClear"
        @cancel="onSearch"
      />
    </div>

    <div class="ctm-content-tabs-sticky">
      <qmp-tabs
        ref="qmpTabs"
        v-model:active="activeTab"
        lazy-render
        @click-tab="onClickTab"
        animated
        class="tab-nav-transparent"
      >
        <qmp-tab v-for="(tab, i) in tabs" :key="i" :title="tab.buttonName" :name="tab.buttonCode">
          <div class="customer-list">
            <StoreUpgraderList :search-value="searchValue" :type="tab.buttonCode" :ref="tab.buttonCode" />
          </div>
        </qmp-tab>
      </qmp-tabs>
    </div>
  </div>
</template>

<script lang="ts">
  import { ref, reactive, toRefs, onMounted, getCurrentInstance, onActivated } from 'vue'
  import { useStore } from 'vuex'
  import SearchInput from '@/components/SearchInput/index.vue'
  import StoreUpgraderList from './components/List.vue'
  import { onBeforeRouteLeave } from 'vue-router'
  import { queryBtnList } from '@/apis/dataDict'

  interface type {
    searchValue: string
    activeTab: string
    tabs: { buttonName: string; buttonCode: string }[]
    listReLoad: boolean
  }
  export default {
    name: 'StoreUpgradeTask',
    components: { SearchInput, StoreUpgraderList },
    setup() {
      const $store = useStore()
      const { proxy, type }: any = getCurrentInstance()
      const qmpTabs = ref()
      const datas = reactive<type>({
        searchValue: '',
        activeTab: '',
        tabs: [],
        listReLoad: false
      })

      // 刷新数据
      const initRefresh = () => {
        proxy.$refs[`${datas.activeTab}`] && proxy.$refs[`${datas.activeTab}`][0].resetPageData()
      }

      const onSearch = () => {
        // 搜索
        initRefresh()
      }
      const onClear = () => {
        // 搜索
      }
      const onClickTab = () => {
        // 点击tab
        initRefresh()
      }

      onBeforeRouteLeave((to) => {
        const instanceName = type?.name
        if (to.name === 'Index') {
          $store.commit('routerKeep/removeCache', instanceName)
        } else {
          $store.commit('routerKeep/resetCache', instanceName)
        }
      })

      proxy.$SimpleEvents.registerEvent('refreshCustomerChangeItem', () => {
        datas.listReLoad = true
      })

      onActivated(() => {
        // 缓存路由才生效
        if (datas.listReLoad) {
          initRefresh()
        }
        datas.listReLoad = false
      })

      // 按钮权限
      const getBtnList = async () => {
        const res: any = await queryBtnList('storeupgrade')
        const { success, data = [] } = res
        if (success === 1) {
          datas.tabs = data
          datas.activeTab = data.length && data[0].buttonCode
        }
      }
      getBtnList()

      onMounted(() => {})
      return {
        ...toRefs(datas),
        qmpTabs,
        onSearch,
        onClear,
        onClickTab
      }
    }
  }
</script>
<style lang="less" scoped>
  .customer-list {
    position: relative;
    padding: 0 14px 58px;
  }
</style>
